<template>
  <!-- 个人信息页面 -->
  <div>
    <div class="header">
      <el-radio-group v-model="tableData" size="small" />
      <div style="margin: 20px" />
      <el-form label-width="80px" :model="formLabelAlign">
        <el-form-item label="登录用户:">{{ user.username }} </el-form-item>
        <el-form-item label="用户姓名:">{{ user.realname }} </el-form-item>
        <el-form-item label="用户手机:">{{ user.telephone }}</el-form-item>
        <el-form-item label="用户性别:">{{ user.gender | genderChange }} </el-form-item>
        <el-form-item label="用户角色:">{{ role }} </el-form-item>
        <el-form-item
          label="用户生日:"
        >{{ user.birth | fmtDate }}
        </el-form-item>
        <el-form-item
          label="注册时间:"
        >{{ user.registerTime | fmtDate }}
        </el-form-item>
        <el-form-item label="邮箱:">{{ email }} </el-form-item>
        <el-form-item
          label="用户状态:"
        ><el-tag type="success">
          {{ user.status }}
        </el-tag></el-form-item>
      </el-form>
    </div>

    <!-- 模态框 -->
    <el-dialog title="提示" :visible.sync="visible" @close="toClose">
      <el-form ref="myForm" :model="form" :rules="rules">
        <el-form-item
          label="登录用户"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="form.username"
            disabled
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="realname"
        >
          <el-input v-model="form.realname" autocomplete="off" />
        </el-form-item>
        <el-form-item label="性别" :label-width="formLabelWidth" prop="gender">
          <el-radio-group v-model="gender">
            <el-radio label="男" />
            <el-radio label="女" />
          </el-radio-group>
        </el-form-item>
        <el-form-item
          label="用户手机"
          :label-width="formLabelWidth"
          prop="telephone"
        >
          <el-input v-model="form.telephone" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="用户邮箱"
          :label-width="formLabelWidth"
          prop="email"
        >
          <el-input v-model="form.email" autocomplete="off" />
        </el-form-item>
        <el-form-item label="出生日期" :label-width="formLabelWidth">
          <el-date-picker
            v-model="value1"
            type="datetime"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="toClose">取 消</el-button>
        <el-button type="primary" @click="toSubmit()">确定修改</el-button>
      </div>
    </el-dialog>
    <!-- 模态框结束 -->
    <template>
      <el-button
        type="text"
        class="amend"
        @click="toEdit(user)"
      ><span>修改个人信息</span></el-button>
    </template>
  </div>
</template>

<script>
import { flatten } from '@antv/util'
import {
  findUserDetailsById,
  saveOrUpdate,
  login,
  getInfo
} from '../../api/user'
import fmtDate from '../../filters'
import genderChange from '../../filters'
import { getToken } from '../../utils/auth'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      tableData: [],
      formLabelAlign: {},
      id: '',
      role: '普通用户',
      // birthday:'暂无'
      email: '暂无',
      // 表单
      form: {},
      formLabelWidth: '120px',
      // 模态框显示与隐藏
      visible: false,
      // 表单校验
      rules: {
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        gender: [{ required: true, message: '请输入姓名', trigger: 'change' }]
      },
      // 日期选择器
      value1: '',
      // 性别
      gender: ''
    }
  },
  computed: {
    ...mapGetters(['user', 'roles'])
  },
  created() {

  },
  mounted() {},
  methods: {
    // 关闭
    toClose() {
      // 重置表单
      setTimeout(() => {
        this.$refs.myForm.resetFields()
        this.visible = false
      }, 500)
    },
    // 更新、保存
    toSubmit() {
      // 校验表单
      this.$refs.myForm.validate(async(valid) => {
        if (valid) {
          // 发送保存或者更新请求
          const res = await saveOrUpdate(this.form)
          this.$notify({
            type: 'success',
            message: res.message
          })
          this.toClose()
        } else {
          return false
        }
      })
    },
    // 修改
    toEdit(user) {
      // console.log(row);
      this.visible = true
      this.form = { ...user }
      if (user.gender === 'male') {
        this.gender = '男'
      } else {
        this.gender = '女'
      }
    }
  }
}
</script>
<style lang="scss">
.header {
  border-bottom: 2px solid #f4f4f4;
}
.amend {
  display: block;
  position: relative;
  left: 1000px;
}
</style>
